import useDictStore from '@/store/modules/dict'
import { getDicts } from '@/api/system/dict/data'

/**
 * 全局数据字典
 */

// 是/否字典
export const sys_yes_no = [
  { label: '是', value: 'Y', elTagType: 'danger' },
  { label: '否', value: 'N', elTagType: 'success' }
];

// 通用状态字典
export const sys_normal_disable = [
  { label: '正常', value: '0', elTagType: 'success' },
  { label: '停用', value: '1', elTagType: 'danger' }
];

/**
 * 获取字典数据
 */
export function useDict(...args) {
  const res = ref({});
  return (() => {
    args.forEach((dictType, index) => {
      res.value[dictType] = [];
      const dicts = useDictStore().getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        // 针对特定的字典类型使用本地定义
        if (dictType === 'sys_yes_no') {
          res.value[dictType] = sys_yes_no;
          useDictStore().setDict(dictType, sys_yes_no);
        } else if (dictType === 'sys_normal_disable') {
          res.value[dictType] = sys_normal_disable;
          useDictStore().setDict(dictType, sys_normal_disable);
        } else {
          // 其他字典从服务器获取
          getDicts(dictType).then(resp => {
            res.value[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
            useDictStore().setDict(dictType, res.value[dictType]);
          })
        }
      }
    })
    return toRefs(res.value);
  })()
}

// 导出所有字典
export default {
  sys_yes_no,
  sys_normal_disable
};